import csv
import os

import requests
from lxml import etree

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
    "Cookie": "ASP.NET_SessionId=moki5bzncstm0lqrxqyleqvl"
}

url = "http://27.223.1.57:10000/PythonApplication/webbasesite/dataInfoList.aspx?oneClassGuid=171030103404278262"

r = requests.get(url=url, headers=headers)

html = etree.HTML(r.text)

# 使用xpath提取内部详细数据
# 分类
td_name = html.xpath('//table/tbody//tr[@class="twoweixuan"]/td/text()')
# 序号
num = html.xpath('//form/table[3]/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td[1]')
# 诉求内容
content = html.xpath('//form/table[3]/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td[2]')
# 诉求时间
r_time = html.xpath('//form/table[3]/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td[3]')
# 回复时间
b_time = html.xpath('//form/table[3]/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td[4]')

# 判断是否存在青岛问政文件夹，如果没有则创建
download_path = os.getcwd() + '/青岛问政/'
if not os.path.exists(download_path):
    os.makedirs(download_path)

head = ['序号', '诉求内容', '诉求时间', '回复时间']

massage_list = []
for i in range(len(num)):
    td_list = []
    td_list.append(num[i])
    td_list.append(num[i])
    td_list.append(num[i])
    td_list.append(num[i])
    massage_list.append(td_list)
print(massage_list)

with open('./青岛问政/QingDao_wenzheng.csv', 'w') as f:
    f_csv = csv.writer(f)
    f_csv.writerow(head)
    f_csv.writerows(massage_list)
